﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;

namespace Aquasoft.Iszr.Proxy
{
    internal static class CertUtils
    {
        internal static X509Certificate2Collection FindCertList(string sn)
        {
            if (string.IsNullOrEmpty(sn))
                return new X509Certificate2Collection();

            X509Certificate2Collection certList;
            var storage = new X509Store(StoreName.My, StoreLocation.CurrentUser);
            try
            {
                storage.Open(OpenFlags.ReadOnly);
                certList = storage.Certificates.Find(X509FindType.FindBySerialNumber, sn, false);
            }
            finally
            {
                storage.Close();
            }

            return certList;
        }

        internal static X509Certificate2 FindCert(string sn)
        {
            var certList = FindCertList(sn);
            if (certList.Count == 1)
                return certList[0];

            return null;
        }

    }
}
